package org.nf.web.servlet.param;

import cn.hutool.core.convert.Convert;
import org.nf.web.servlet.MethodArgumentResolver;
import org.nf.web.servlet.ServletApi;
import org.nf.web.servlet.annotation.Header;

import java.lang.reflect.Parameter;

/**
 * @author wangliang
 * @date 2024/6/14
 */
public class HeaderMethodArgumentResolver implements MethodArgumentResolver {

    @Override
    public boolean supports(Parameter parameter) {
        return parameter.isAnnotationPresent(Header.class);
    }

    @Override
    public Object resolve(Parameter parameter, ServletApi servletApi) throws Exception {
        String headerName = parameter.getAnnotation(Header.class).value();
        String headerValue = servletApi.request().getHeader(headerName);
        return Convert.convert(parameter.getType(), headerValue);
    }
}
